home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Trading on the Edge
/
Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin
/
pc
/
mac_file
/
vendor_d
/
neuralwa
/
nw2v50
/
lpn_demo.c
< prev
next >
Wrap
Text File
|
1993-08-23
|
11KB
|
475 lines
/* lpn_demo.c */
/* LOGICON PROJECTION NETWORK (TM) Circle/half-plane demo */
/************************************************************************
* Copyright(C) 1987-1992 NeuralWare Inc *
* Penn Center West, IV-227, Pittsburgh, PA 15276 *
* Telephone: (412) 787-8222 FAX: (412) 787-8220 *
* *
* All rights reserved. No part of this program may be reproduced, *
* stored in a retrieval system, or transmitted, in any form or by any *
* means, electronic, mechanical, photocopying, recording or otherwise *
* without the prior written permission of the copyright owner, *
* NeuralWare, Inc. *
* *
* PROPRIETARY NOTICE *
* *
* This document is the property of NeuralWare, Inc. and contains *
* trade-secrets and other proprietary information. The information *
* herein is reserved as proprietary to NeuralWare, and is not to be *
* published, reproduced, copied, disclosed, used, or reverse *
* engineered without the express written consent of a duly authorized *
* representative of NeuralWare. *
************************************************************************
*/
#define UIO_SERVER 1
#define SERVER_EMULATOR_FILE 1
#define SKIP_COMPAT_DATA 1
#include "userutl.h"
#include "uio_strc.h"
#include "uio_tran.h"
#include "uio_reqs.pro"
#include "uio_cmds.pro"
#include <math.h>
#ifdef MAC
#include "macuio.redef"
#endif
int W_Wid = {400}; /* basic defining parameters */
int W_Ht = {200}; /* height of graph */
int count = 0;
int good_count = 0;
float xin, yin, desval;
TEXT sbuf[200];
int circ_color;
int back_color;
int on_color;
int off_color;
int savebestf; /* Save best flag */
float clssrate; /* Classification rate */
#define W0 1
#define W1 2
#define UPDT_INTERVAL 100
#define BIAS_LYRX (-1)
#define PROJ_LYRX 1
#define PROTO_LYRX 2
NINT UIO_Init(file)
TEXT *file;
{
NINT ypos;
NINT ret_val = UIO_OK;
NINT xsize, ysize, ncolor, chrx, chry; /* graphics parameters */
savebestf = 0;
/* Get screen parameters */
ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry);
if ( ncolor < 8 ) {
circ_color = 1;
back_color = 0;
on_color = 1;
off_color = 0;
} else {
circ_color = 1;
back_color = 0;
on_color = 6;
off_color = 5;
}
ypos = ysize - W_Ht - 100;
if (ypos < 75)
ypos = 75;
if (ypos > 300)
ypos = 300;
/* Set up windows */
ug_window(W0, back_color, 20, ypos+25, 20+W_Wid, ypos+25+W_Ht);
ug_window(W1, back_color, 20, ypos, 20+W_Wid, ypos+24);
return(ret_val);
}
NINT UIO_Term(process)
NINT process;
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Attention()
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Learn_Start()
{
NINT ret_val = UIO_OK;
/* Set up learn file */
ug_fileio("crcpltrn", 0, 1, 1, 1);
count = 0;
return(ret_val);
}
NINT UIO_Learn_Input(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Learn_Output(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Learn_Result(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
ALYR alyr; /* layer info */
NINT n_cn_vals;
SREAL cn_vals[10]; /* Connection values */
SREAL bias; /* Bias */
SREAL thrsh, wr0, wr; /* Projection work values */
SREAL Rad, Rado; /* Projection radius and distance */
SREAL cntr_x, cntr_y; /* Center of unprojected prototype */
SREAL prad; /* Radius of unprojected prototype */
SREAL nrm; /* norm of connections */
CTRIO_T ctrs[8]; /* Counters */
NINT wx,wy; /* Work indices */
NINT gcx,gcy,grad; /* Graphical circle params */
NINT ret_val = UIO_OK;
count++;
if (count !=1 && (count%UPDT_INTERVAL) != 0)
return(ret_val);
ug_winclr(W0);
ug_winclr(W1);
ug_circle(W0,circ_color,0,
(NINT)(0.35*W_Wid), (NINT)(0.50*W_Ht), (NINT)(0.4*W_Ht),
circ_color==on_color?0:1);
if (circ_color==on_color)
ug_line(W0,circ_color,0,(NINT)(0.75*W_Wid),0,(NINT)(0.75*W_Wid),W_Ht,0);
else
ug_boxf(W0,circ_color,0,(NINT)(0.75*W_Wid),1,W_Wid,W_Ht);
/* Read counters */
ug_ctrio(ctrs,0);
sprintf(sbuf,"Logicon Projection Network (TM). Lrn Cnt:%7ld",
ctrs[2]);
ug_puts(W1,on_color,0,6,4,sbuf,0);
/* Get info from projection layer. */
ug_lyrio(&alyr, 0, PROJ_LYRX);
Rad = alyr.scale;
Rado = alyr.offst;
/* Get info from prototype layer */
ug_lyrio(&alyr, 0, PROTO_LYRX);
/* Now traverse the prototype layer */
for (wx=0; wx<alyr.num_pes; wx++) {
/* Get connections from projection layer; there should be 3 */
n_cn_vals = 3; /* 3 pes in projection layer */
ug_cnio(PROTO_LYRX,wx,PROJ_LYRX,0,&n_cn_vals,0,0,cn_vals);
if (n_cn_vals != 3)
break;
n_cn_vals = 1;
ug_cnio(PROTO_LYRX,wx,BIAS_LYRX,0,&n_cn_vals,0,0,&bias);
if (n_cn_vals != 1)
break;
/* If the prototypes are not getting projected back
to the hypersphere, we should project them back */
nrm=0.0;
/* Calculate radius of hyperwhere current weight sits on */
for (wy=0; wy<3; wy++)
nrm += cn_vals[wy]*cn_vals[wy];
if (fabs(nrm) < NRZRO)
break;
nrm = Rad/sqrt(nrm);
/* Scale the whole thing down, including threshold */
for (wy=0; wy<3; wy++)
cn_vals[wy] *= nrm;
bias *= nrm;
thrsh = 0.0-bias;
wr0 = 0.0-cn_vals[2];
wr = wr0+(thrsh/Rad);
if (fabs(wr) > NRZRO) {
NINT on_sphere;
wr = Rado/wr;
cntr_x = wr*cn_vals[0];
cntr_y = wr*cn_vals[1];
prad = thrsh/(Rad*Rad);
prad = 1.0 - (prad*prad);
if (prad > NRZRO) {
prad = wr*Rad*sqrt(prad);
on_sphere = 1;
} else
on_sphere = 0;
/* Following assumes that data is between -1 and 1
for x, and between -0.5 and 0.5 for y */
gcx = 0.5*(cntr_x+1.0)*W_Wid;
gcy = (cntr_y+0.5)*W_Ht;
if (on_sphere) {
if (prad < 0.0)
prad = 0.0-prad;
grad = prad*W_Ht;
ug_circle(W0,on_color,0,gcx,gcy,grad,0);
} else {
/* Prototype radius too large; draw cross-hair */
ug_line(W0,on_color,0,gcx-10,gcy,gcx+10,gcy,0);
ug_line(W0,on_color,0,gcx,gcy-10,gcx,gcy+10,0);
}
#if 0 /* For debugging */
{
TEXT *sp;
sprintf(sbuf,"thrs=%f,wr0=%f,wr=%f,cx=%f,cy=%f,rad=%f,nrm=%f",
thrsh,wr0,wr,cntr_x,cntr_y,prad,nrm);
PutStr(sbuf);
sp=GetStr( );
if (*sp=='q' || *sp=='Q')
ret_val = UIO_EXIT;
PutStr("\n");
}
#endif
}
}
return(ret_val);
}
NINT UIO_Learn_End()
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Recall_Start()
{
NINT ret_val = UIO_OK;
CTRIO_T ctrs[8]; /* Counters */
TEXT *sp;
if (savebestf) {
ug_fileio("crcpltst", 1, 0, 0, 0);
} else {
PutStr("Test Set (T) or Training Set (L)?: ");
sp = GetStr( );
if (sp[0] == 'l' || sp[0] == 'L')
ug_fileio("crcpltrn", 1, 0, 0, 0);
else
ug_fileio("crcpltst", 1, 0, 0, 0);
}
ug_winclr(W0);
ug_winclr(W1);
ug_circle(W0,on_color,0,(NINT)(0.35*W_Wid), (NINT)(0.50*W_Ht),
(NINT)(0.4*W_Ht),0);
ug_line(W0,on_color,0,(NINT)(0.75*W_Wid),0,(NINT)(0.75*W_Wid),W_Ht,0);
if (savebestf) {
/* Read counters */
ug_ctrio(ctrs,0);
sprintf(sbuf,"Logicon Projection Network (TM). Lrn Cnt:%7ld",
ctrs[2]);
ug_puts(W1,on_color,0,6,4,sbuf,0);
}
count = 0;
good_count = 0;
return(ret_val);
}
NINT UIO_Read(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
/* Since this is file IO, the data should already be in the buffer */
xin = Datap[0];
yin = Datap[1];
desval = -2.0;
return(ret_val);
}
NINT UIO_Write(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
SREAL outval;
NINT x0, y0;
outval = Datap[0];
x0 = (xin+1.0) * W_Wid / 2;
y0 = (yin+0.5) * W_Ht;
#if 0
sprintf(sbuf,"%f %f %f %f\n ", xin, yin, outval, desval);
PutStr(sbuf);
GetStr( );
#endif
ug_box(W0, outval>0.5?on_color:off_color,0,
x0-1,y0-1,x0+1,y0+1,0);
if (desval >-1.5) {
count ++;
/* Assume sigmoid output */
if ((desval >0.5 && outval >0.5) ||
(desval <0.5 && outval <0.5))
good_count++;
}
return(ret_val);
}
NINT UIO_Write_Step(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Recall_Test(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
/* Since this is file IO, the data should already be in the buffer */
desval = Datap[0];
return(ret_val);
}
NINT UIO_Recall_End()
{
NINT ret_val = UIO_OK;
ug_circle(W0,on_color,0,(NINT)(0.35*W_Wid), (NINT)(0.50*W_Ht),
(NINT)(0.4*W_Ht),0);
ug_line(W0,on_color,0,(NINT)(0.75*W_Wid),0,(NINT)(0.75*W_Wid),W_Ht,0);
clssrate = 0.0;
if (count > 0) {
clssrate = ((float)good_count) / ((float)count);
if (!savebestf) {
sprintf(sbuf,"Logicon Projection Network (TM). %5.2f%% Correct",
100.0 * clssrate );
ug_puts(W1,on_color,0,15,4,sbuf,0);
}
}
if (!savebestf) {
PutStr("Hit <Enter> to continue:");
GetStr( );
}
return(ret_val);
}
NINT UIO_Instrument(Instrument_id, nDataElems, DataElemp)
NINT Instrument_id;
NINT nDataElems;
SREAL *DataElemp;
{
NINT ret_val = UIO_OK;
return(ret_val);
}
/* FUNCTION */
NINT UIO_ObjFunc(eoeflag, DataElemp)
NINT eoeflag;
SREAL *DataElemp;
{
NINT ret_val = UIO_OK;
if (eoeflag) {
savebestf = 0; /* Clear save best flag */
/* Use classification rate as save best objective function */
/* Need to pass back 1-clssrate because assumption by NWorks
is the smaller the better */
*DataElemp = 1.0-clssrate;
} else
savebestf = 1; /* Tell recall we are in savebest mode */
return(ret_val);
}
NINT UIO_Rewind()
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Explain(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
return(ret_val);
}